package com.gleason.gt;


import com.gleason.gt.service.IBackgroundImageService;
import com.gleason.gt.service.IMusicService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RadioActivity extends Activity {
	private IMusicService musicService = null;
	private IBackgroundImageService backgroundImageService = null;
	private LinearLayout view;	
	private Bitmap currentBackground;
	private Boolean musicStarted = false;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_layout);
        view = (LinearLayout)findViewById(R.id.mainLayout);
        ImageButton button = (ImageButton)findViewById(R.id.toggleMusic);
        button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					if(musicStarted){
						musicService.pause();
						setPause();
					}
					else{
						musicService.play();
						setPlay();
					}
					
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
				}
			}
		});
        try{
        	getApplicationContext().bindService(new Intent(IBackgroundImageService.class.getName()),
        			 serConnImage, Context.BIND_AUTO_CREATE);
        	getApplicationContext().bindService(new Intent(IMusicService.class.getName()),
      			 serConnMusic, Context.BIND_AUTO_CREATE);
        }
        catch(RuntimeException re){
        	re.printStackTrace();
        } 
        
    }
    
    private void setImage() throws RemoteException{
    	if(currentBackground != null){
    		currentBackground.recycle();
    	}
    	currentBackground = BitmapFactory.decodeFile(backgroundImageService.getCurrentImageLocation());
    	view.setBackgroundDrawable(new BitmapDrawable(currentBackground));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
    	try {
    		switch (item.getItemId()) {
//        		case R.id.skipLeft:
//        			musicService.skipLeft();
//        			setTitle();
//        			break;
        		case R.id.toggleImg:
        			setImage();
        			break;
//        		case R.id.play:
//        			musicService.play();
//        			setTitle();
//        			break;
//        		case R.id.next:
//        			musicService.skipRight();
//        			setTitle();
//        			break;
        		default:
        			return super.onOptionsItemSelected(item);
    		}
    		return true;
        } catch (RemoteException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
        	return false;
		}
    }
    private ServiceConnection serConnMusic = new ServiceConnection() {
      	 @Override
      	 public void onServiceConnected(ComponentName name, IBinder service) {
      		 musicService = IMusicService.Stub.asInterface(service);
      	 }

      	 @Override
      	 public void onServiceDisconnected(ComponentName name) {
      		 musicService = null;
      	 }
    };
    private ServiceConnection serConnImage = new ServiceConnection() {
     	 @Override
     	 public void onServiceConnected(ComponentName name, IBinder service) {
     		 backgroundImageService = IBackgroundImageService.Stub.asInterface(service);
     	 }

     	 @Override
     	 public void onServiceDisconnected(ComponentName name) {
     		backgroundImageService = null;
     	 }
   };
    private void setTitle(){
    	TextView view = (TextView)findViewById(R.id.songTitle);
    	try {
			view.setText(musicService.getTitle());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
    }
    private void setPause(){
    	TextView view = (TextView)findViewById(R.id.songTitle);
    	view.setText("Paused");
    }
    private void setPlay(){
    	TextView view = (TextView)findViewById(R.id.songTitle);
    	view.setText("Push To Start");
    }
}
